package com.example.apkmanage.adapter;

import java.lang.ref.SoftReference;
import java.util.HashMap;

import android.text.TextUtils;

public class ResponseCacheManager {

	private static ResponseCacheManager mInstance;
	private static Object mLock = new Object();

	private HashMap mResponsePool;
	private SoftReference<HashMap> mResponseCache;

	private ResponseCacheManager() {
		mResponsePool = new HashMap();
		mResponseCache = new SoftReference<HashMap>(mResponsePool);
	}

	public static ResponseCacheManager getInstance() {
		synchronized (mLock) {
			if (mInstance == null) {
				mInstance = new ResponseCacheManager();
			}
		}
		return mInstance;
	}

	public Object getResponse(String key) {

		if (TextUtils.isEmpty(key))
			return null;

		if (mResponseCache == null) {
			return null;
		}

		return mResponseCache.get().get(key);
	}

	public void putResponse(String key, Object value) {
		if (mResponseCache != null) {
			mResponseCache.get().put(key, value);
		}
	}

	public void clear() {
		if (mResponseCache != null) {
			mResponseCache.clear();
			mResponseCache = null;
		}
		if (mResponsePool != null) {
			mResponsePool.clear();
			mResponsePool = null;
		}
		mInstance = null;
	}

}